package com.appspot.didit.test.datastore;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import com.appspot.didit.client.tag.Tag;
import com.appspot.didit.server.PMF;

public class TagDatastoreTestCase extends LocalDatastoreTestCase {

	PersistenceManager pm = PMF.get().getPersistenceManager();

	public void testInsertAndSelect() {
	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			//******************************
	    	Tag tag1 = new Tag("test", user_id, "");
	    	pm.makePersistent(tag1);
	    	Tag tag2 = pm.getObjectById(Tag.class, tag1.getId());
	    	assertEquals("test", tag2.getTagname());
	    	assertEquals(user_id, tag2.getCreator_id());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	public void testUpdate() {
	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
	    	Tag tag0 = new Tag("test", user_id, "");
	    	pm.makePersistent(tag0);
	    	//****************************
	    	tag0.setTagname("test1");
	    	
	    	Tag tag1 = pm.getObjectById(Tag.class, tag0.getId());
	    	assertEquals(tag1.getId(), tag0.getId());
	    	assertEquals(user_id, tag1.getCreator_id());
	    	assertEquals("test1", tag1.getTagname());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
		
	}
	
	public void testDelete() {
		try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
	    	Tag tag0 = new Tag("test", user_id, "");
	    	pm.makePersistent(tag0);
	    	//****************************
	    	Tag tag1 = pm.getObjectById(Tag.class, tag0.getId());
	    	pm.deletePersistent(tag1);
	    	List<Tag> tags = getResults(user_id);
	    	assertNotNull(tags);
	    	assertEquals(0, tags.size());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	private List<Tag> getResults(Long user_id) {
		String sql = "SELECT FROM " + Tag.class.getName() + " where creator_id == " + user_id;
		List<Tag> results = (List<Tag>) pm.newQuery(sql).execute();
		return results;
	}
	
}
